<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
    body, html {
        width: 100%;
        height: 100%;
        margin: 0;
    }
    #allmap {
        height: 100%;
        width: 100%;
    }
    ul li {
        list-style: none;
    }
    .btn-wrap {
        z-index: 999;
        position: fixed;
        bottom: 3.5rem;
        margin-left: 3rem;
        padding: 1rem 1rem;
        border-radius: .25rem;
        background-color: #fff;
        box-shadow: 0 2px 6px 0 rgba(27, 142, 236, 0.5);
    }
    .btn {
        width: 75px;
        height: 30px;
        float: left;
        background-color: #fff;
        color: rgba(27, 142, 236, 1);
        font-size: 14px;
        border:1px solid rgba(27, 142, 236, 1);
        border-radius: 5px;
        margin: 0 5px;
        text-align: center;
        line-height: 30px;
    }
    .btn:hover {
        background-color: rgba(27, 142, 236, 0.8);
        color: #fff;
    }
</style>
    <script type="text/javascript" src="//api.map.baidu.com/api?type=webgl&v=1.0&ak=您的密钥"></script>
    <title>添加/清除覆盖物</title>
</head>
<body>
    <div id="allmap"></div>
    <ul class="btn-wrap" style="z-index: 99;">
        <li class = "light btn" onclick = "addOverlay()">添加覆盖物</li>
        <li class = "night btn" onclick = "removeOverlay()">清除覆盖物</li>
    </ul>
</body>
</html>
    <script type="text/javascript">
    var map = new BMapGL.Map('allmap');
    var point = new BMapGL.Point(116.404, 39.915);
    map.centerAndZoom(point, 15);
    // 创建点
    var marker = new BMapGL.Marker(new BMapGL.Point(116.404, 39.915));
    // 创建折线
    var polyline = new BMapGL.Polyline([
        new BMapGL.Point(116.399, 39.910),
        new BMapGL.Point(116.405, 39.920),
        new BMapGL.Point(116.425, 39.900)
    ], {
        strokeColor: 'blue',
        strokeWeight: 2,
        strokeOpacity: 0.5
    });
    // 创建圆
    var circle = new BMapGL.Circle(point, 500, {
        strokeColor: 'blue',
        strokeWeight: 2,
        strokeOpacity: 0.5
    });
    // 创建多边形
    var polygon = new BMapGL.Polygon([
        new BMapGL.Point(116.387112, 39.920977),
        new BMapGL.Point(116.385243, 39.913063),
        new BMapGL.Point(116.394226, 39.917988),
        new BMapGL.Point(116.401772, 39.921364),
        new BMapGL.Point(116.41248, 39.927893)
    ], {
        strokeColor: 'blue',
        strokeWeight: 2,
        strokeOpacity: 0.5
    });
    // 创建矩形
    var pStart = new BMapGL.Point(116.392214, 39.918985);
    var pEnd = new BMapGL.Point(116.41478, 39.911901);
    var rectangle = new BMapGL.Polygon([
        new BMapGL.Point(pStart.lng, pStart.lat),
        new BMapGL.Point(pEnd.lng, pStart.lat),
        new BMapGL.Point(pEnd.lng, pEnd.lat),
        new BMapGL.Point(pStart.lng, pEnd.lat)
    ], {
        strokeColor: 'blue',
        strokeWeight: 2,
        strokeOpacity: 0.5
    });
    // 添加覆盖物
    function addOverlay() {
        map.addOverlay(marker); // 增加点
        map.addOverlay(polyline); // 增加折线
        map.addOverlay(circle); // 增加圆
        map.addOverlay(polygon); // 增加多边形
        map.addOverlay(rectangle); // 增加矩形
    }
    // 清除覆盖物
    function removeOverlay() {
        map.clearOverlays();
    }
    addOverlay();
    </script>